package Frame;
import DataBase.SQL_Readin;
import InterFace.BaseWin_api;
import MeHua.*;
import pojo.Pw_Hash;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.util.Objects;

public class BaseWin implements BaseWin_api {//郭
    SQL_Readin db = new SQL_Readin();
    JButton b_return = new JButton("返回");
    JFrame BaseWin = new JFrame("高校班级管理系统");
    NewPanel BasePanel = new NewPanel();
    NewLogPanel log_Panel = new NewLogPanel();
    NewSignPanel sign_Panel = new NewSignPanel();
    NewButton b_lon = new NewButton("用户登录",50,30,UFONT.getFont1());
    NewButton b_sign = new NewButton("用户注册",50,30,UFONT.getFont1());
    //登录面板中
    JLabel log_label1 = new JLabel("登录账号:");
    JLabel log_label2 =new JLabel("登录密码:");
    NewButton log_b_log = new NewButton("登录",50,30,UFONT.getFont1());
    JRadioButton r_b_man = new JRadioButton("管理用户");
    JRadioButton r_b_nor = new JRadioButton("普通用户");
    ButtonGroup log_group = new ButtonGroup();
    JTextField log_text_num = new JTextField();
    JPasswordField log_text_pw = new JPasswordField();
    //注册面板中
    JLabel sign_label1 = new JLabel("注册账号:");
    JLabel sign_label2 = new JLabel("注册密码:");
    JTextField sign_text_num = new JTextField();
    JTextField sign_text_pw = new JTextField();
    NewButton sign_b_sign = new NewButton("注册",50,30,UFONT.getFont1());
    Beautify beautify=new Beautify();
    public void creat(){//郭
        BaseWin.setLayout(null);
        BaseWin.setBounds(400,100,800,600);
        BasePanel.setLayout(null);
        BasePanel.setBounds(0,0,800,600);
        BasePanel.setBackground(new Color(229, 234, 224));
        BaseWin.add(BasePanel);
        BaseWin.setResizable(false);
        BaseWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b_lon.setBounds(300,300,200,80);
        beautify.MeHuaB(b_lon,194,192,192,30);
        b_sign.setBounds(300,400,200,80);
        beautify.MeHuaB(b_sign,194,192,192,30);

        BasePanel.add(b_lon);
        BasePanel.add(b_sign);
        b_lon.addActionListener(e -> {
            //便签字体
            log_label1.setFont(UFONT.getFont2());
            log_label2.setFont(UFONT.getFont2());
            BaseWin.setBounds(500,100,420,600);
            BaseWin.remove(BasePanel);
            BaseWin.repaint();
            log_Panel.add(b_return);
            b_return.setBounds(10,10,80,60);
            r_b_man.setBounds(50,350,80,50);
            r_b_nor.setBounds(250,350,80,50);
            r_b_nor.setSelected(true);
            log_group.add(r_b_man);
            log_group.add(r_b_nor);
            log_Panel.add(r_b_man);
            log_Panel.add(r_b_nor);
            log_Panel.setLayout(null);
            log_b_log.setBounds(90,430,200,70);
            log_Panel.add(log_b_log);
            log_text_num.setBounds(120,200,200,50);
            log_text_pw.setBounds(120,300,200,50);
            log_Panel.add(log_text_num);
            log_Panel.add(log_text_pw);
            log_label1.setBounds(20,200,100,60);
            log_Panel.add(log_label1);
            log_label2.setBounds(20,300,100,60);
            log_Panel.add(log_label2);
            log_Panel.setBounds(0,0,400,600);
            BaseWin.add(log_Panel);
            b_return.addActionListener(e1 -> {
                fun_return();
            });
            //登录
            log_b_log.addActionListener(e1 ->{
                if(log_text_num.getText().isEmpty() || log_text_pw.getText().isEmpty()){
                    JOptionPane.showMessageDialog(null,"请填入完整信息");
                }
                else {
                    String num = log_text_num.getText();
                    String pw = String.valueOf(log_text_pw.getPassword());
                    String id;
                    String pw_hash = Pw_Hash.pw_hash(pw);
                    if (r_b_man.isSelected()) {
                        try {
                            id = db.userLog(num, pw_hash, "user_man");
                            if (!id.equals("**/**")) {
                                toManWin(num, id);
                            }
                            else JOptionPane.showMessageDialog(null,"登录失败，检查是否填错信息","登录失败",JOptionPane.ERROR_MESSAGE);
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                    } else if (r_b_nor.isSelected()) {
                        try {
                            id = db.userLog(num, pw_hash, "user_nor");
                            if (!id.equals("**/**")) {
                                toNorWin(num, id);
                            }
                            else JOptionPane.showMessageDialog(null,"登录失败,检查是否填错信息","登录失败",JOptionPane.ERROR_MESSAGE);
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            });
        });
        b_sign.addActionListener(e ->{
            sign_label1.setFont(UFONT.getFont2());
            sign_label2.setFont(UFONT.getFont2());
            BaseWin.setBounds(500,100,420,600);
            BaseWin.remove(BasePanel);
            BaseWin.repaint();
            sign_Panel.add(b_return);
            b_return.setBounds(10,10,80,60);
            log_group.add(r_b_man);
            log_group.add(r_b_nor);
            sign_Panel.add(r_b_man);
            sign_Panel.add(r_b_nor);
            sign_Panel.setLayout(null);
            sign_Panel.setBounds(0,0,400,600);
            sign_text_num.setBounds(120,200,200,50);
            sign_text_pw.setBounds(120,280,200,50);
            sign_Panel.add(sign_text_num);
            sign_Panel.add(sign_text_pw);
            sign_label1.setBounds(20,200,100,60);
            sign_label2.setBounds(20,280,100,60);
            sign_Panel.add(sign_label1);
            sign_Panel.add(sign_label2);
            r_b_man.setBounds(50,400,80,50);
            r_b_nor.setBounds(250,400,80,50);
            r_b_nor.setSelected(true);
            sign_b_sign.setBounds(90,480,200,70);
            sign_Panel.add(sign_b_sign);
            //班级下拉框
            JLabel l_class = new JLabel("选择所属班级");
            l_class.setBounds(30,350,100,50);
            JComboBox<String> combo = new JComboBox<>();
            combo.setBounds(150,360,100,30);
            String[] class_id = db.readClassID();
            for (String string : class_id) {
                combo.addItem(string);
            }
            sign_Panel.add(combo);
            sign_Panel.add(l_class);
            BaseWin.add(sign_Panel);
            b_return.addActionListener(e1 -> {
                fun_return();
            });
            //用户注册
            sign_b_sign.addActionListener(e1 -> {
                if (sign_text_num.getText().isEmpty() || sign_text_pw.getText().isEmpty()) {
                    JOptionPane.showMessageDialog(null,"请填入完整信息!");
                }
                else {
                    String num = sign_text_num.getText();
                    String pw = sign_text_pw.getText();
                    String id = Objects.requireNonNull(combo.getSelectedItem()).toString();
                    String pw_hash = Pw_Hash.pw_hash(pw);
                    if (r_b_man.isSelected()) {
                        if (db.userSign(num, pw_hash, id, "user_man")) {
                            JOptionPane.showMessageDialog(null, "注册成功");
                            fun_return();
                        } else JOptionPane.showMessageDialog(null, "注册失败");
                    } else if (r_b_nor.isSelected()) {
                        if (db.userSign(num, pw_hash, id, "user_nor")) {
                            JOptionPane.showMessageDialog(null, "注册成功");
                            fun_return();
                        } else JOptionPane.showMessageDialog(null, "注册失败");
                    }
                }
            });
        });
        BaseWin.setVisible(true);
    }
    void fun_return(){//郭
        BaseWin.setBounds(400,100,800,600);
        BaseWin.remove(log_Panel);
        BaseWin.remove(sign_Panel);
        BaseWin.repaint();
        BaseWin.add(BasePanel);
    }
    private void toManWin(String number,String id){//郭
        ManWin manwin = new ManWin(number,id);
        manwin.creat();
        BaseWin.setVisible(false);
    }
    private void toNorWin(String number,String id){//郭
        NorWin norwin = new NorWin(number,id);
        norwin.creat();
        BaseWin.setVisible(false);
    }
}
